package com.beaut.project.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;


public class ControlUtil
{
    private static final Logger logger = Logger.getLogger(ControlUtil.class);

    /**
     * 添加分页的数据项begin end
     * 
     * @param request
     * @return
     */
    public static Map<String, Object> listMap(HttpServletRequest request)
    {
        int page = Integer.parseInt(request.getParameter("page"));
        int rows = Integer.parseInt(request.getParameter("rows"));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("begin", (page - 1) * rows>=0?(page - 1) * rows:0);
        map.put("end", rows);
        logger.debug(map);
        return map;
    }
    
    /**
     * 将request参数转换成Map，包含分页的参数
     * @param request
     * @return
     */
    public static Map<String, Object> paginationMap(HttpServletRequest request){
    	Map<String, Object> m = requestParameterToMap(request);
    	m.putAll(listMap(request));
    	return m;
    }

    /**
     * 将request参数转换成Map
     * @param request
     * @return
     */
    public static Map<String, Object> requestParameterToMap(HttpServletRequest request)
    {
        Map<String, Object> m = new HashMap<String, Object>();
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements())
        {
            String name = names.nextElement();
            m.put(name, request.getParameter(name).trim());
        }
        logger.debug(m);
        return m;
    }
}
